package com.jxt.myjxtmanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.views.CustomSelectDialog;

import java.util.List;


public class DialogSelectAdapter extends BaseAdapter {


    private List<String> mData;
    private CustomSelectDialog.OnClickCityItem instance;
    Context context;

    public DialogSelectAdapter(Context context, List<String> mData) {
        this.mData = mData;
        this.context = context;
    }

    /**
     * 设置监听回调
     *
     * @param instance
     */
    public void setOnClickInstance(CustomSelectDialog.OnClickCityItem instance) {
        this.instance = instance;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolderSelect holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dialog_select, null);
            holder = new ViewHolderSelect(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderSelect) convertView.getTag();
        }
        holder.tv_city.setText(mData.get(position));
        holder.tv_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (instance != null) {
                    instance.onItemClick(position);
                }
            }
        });
        return convertView;
    }

    public class ViewHolderSelect {
        TextView tv_city;//s城市简称码

        public ViewHolderSelect(View itemView) {
            tv_city = (TextView) itemView.findViewById(R.id.tv_dialog_select);
        }
    }

}
